//package com.franklin.ideaplugin.easytesting.editor;
//
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.util.StrUtil;
//import com.franklin.ideaplugin.easytesting.vfs.EasyTestingVirtualFileSystem;
//import com.franklin.ideaplugin.easytesting.vfs.ExecuteMethodScriptVirtualFile;
//import com.franklin.ideaplugin.easytesting.vfs.ExecuteMethodVirtualFile;
//import com.intellij.ide.highlighter.JavaFileType;
//import com.intellij.openapi.application.WriteAction;
//import com.intellij.openapi.editor.Document;
//import com.intellij.openapi.editor.event.EditorMouseEvent;
//import com.intellij.openapi.editor.event.EditorMouseMotionListener;
//import com.intellij.openapi.editor.ex.EditorEx;
//import com.intellij.openapi.editor.impl.DocumentImpl;
//import com.intellij.openapi.fileEditor.FileDocumentManager;
//import com.intellij.openapi.fileEditor.FileEditor;
//import com.intellij.openapi.fileEditor.impl.FileDocumentManagerBase;
//import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl;
//import com.intellij.openapi.project.Project;
//import com.intellij.openapi.vcs.FileStatus;
//import com.intellij.openapi.vcs.FileStatusManager;
//import com.intellij.openapi.vfs.VirtualFile;
//import com.intellij.openapi.vfs.VirtualFileManager;
//import com.intellij.openapi.vfs.VirtualFileSystem;
//import com.intellij.openapi.vfs.newvfs.impl.VirtualFileImpl;
//import com.intellij.psi.*;
//import com.intellij.psi.impl.source.PsiJavaFileImpl;
//import com.intellij.testFramework.LightVirtualFile;
//import com.intellij.util.LocalTimeCounter;
//import org.jetbrains.annotations.NotNull;
//
//import java.lang.reflect.Field;
//import java.nio.charset.StandardCharsets;
//import java.util.Objects;
//import java.util.Set;
//
///**
// * @author Franklin
// * @since 2023/07/30 09:05
// */
//public class ExecuteMethodScriptEditorProvider extends BaseExecuteMethodScriptEditorProvider {
//
//    @Override
//    protected @NotNull FileEditor buildEditor(@NotNull Project project, @NotNull ExecuteMethodVirtualFile executeMethodVirtualFile) {
//        PsiMethod interfaceClassName = executeMethodVirtualFile.getPsiMethod();
//        PsiFileFactory factory = PsiFileFactory.getInstance(project);
//        VirtualFile scriptFile = getScriptFile(project, executeMethodVirtualFile);
//        String path = scriptFile.getPath();
//        String code = FileUtil.readString(path,StandardCharsets.UTF_8);
//
//        String className = ExecuteMethodTransformerBuilder.buildClassName(interfaceClassName);
//        String fileName = className + ".java";
//
//        //创建默认文件
//        JavaFileType javaFileType = JavaFileType.INSTANCE;
//        final long stamp = LocalTimeCounter.currentTime();
//        PsiFile javaFile = factory.createFileFromText(fileName, javaFileType,code , stamp, true, false);
////        scriptFile = javaFile.getVirtualFile();
//        PsiManager psiManager = PsiManager.getInstance(project);
//        PsiJavaFileImpl psiJavaFile = (PsiJavaFileImpl) javaFile;
//
//        PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
//
//        VirtualFile virtualFile = javaFile.getVirtualFile();
//        EasyTestingVirtualFileSystem fileSystem = EasyTestingVirtualFileSystem.getInstance();
//        fileSystem.cacheFile(virtualFile);
//        BaseScriptTextEditor baseScriptTextEditor = new BaseScriptTextEditor(project, virtualFile, this){
//            @Override
//            protected String getRealName() {
//                return "Script Editor";
//            }
//
//            @Override
//            public void refreshText(String newText) {
//                try {
//                    WriteAction.run(() -> {
//                        psiDocumentManager.getDocument(psiJavaFile).setText(newText);
//                    });
//                } catch (Throwable throwable) {
//                    //ignore
//                }
//            }
//
//            @Override
//            public void dispose() {
//                fileSystem.removeFile(virtualFile);
//                super.dispose();
//            }
//        };
//        baseScriptTextEditor.addButton(executeMethodVirtualFile.getExecuteHistoryFormNew().getExecuteBtn());
//        baseScriptTextEditor.addButton(executeMethodVirtualFile.getExecuteHistoryFormNew().getRollbackExecuteBtn());
//        baseScriptTextEditor.addButton(executeMethodVirtualFile.getExecuteHistoryFormNew().getDownloadBtn());
//        baseScriptTextEditor.initToolBar();
//
//        EditorEx editor = (EditorEx) baseScriptTextEditor.getEditor();
//        editor.addEditorMouseMotionListener(new EditorMouseMotionListener() {
//            @Override
//            public void mouseMoved(@NotNull EditorMouseEvent e) {
//                WriteAction.run(() -> {
//                    try {
//                        String newText = javaFile.getText();
//                        VirtualFile virtualFile = VirtualFileManager.getInstance().refreshAndFindFileByNioPath(FileUtil.file(path).toPath());
//                        FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
//                        if (Objects.isNull(virtualFile)){
//                            return;
//                        }
//                        Document document = fileDocumentManager.getDocument(virtualFile);
//                        if (Objects.isNull(document) || newText.equals(document.getText())){
//                            return;
//                        }
//                        document.setText(newText);
//                        fileDocumentManager.saveDocument(document);
//                        FileUtil.writeString(newText,path,StandardCharsets.UTF_8);
//                        ((VirtualFileImpl) virtualFile).setModificationStamp(document.getModificationStamp());
//                    } catch (Throwable throwable) {
//                        //ignore
//                        throwable.printStackTrace();
//                    }
//                });
//            }
//        });
//        return baseScriptTextEditor;
//    }
//
//
//}
